package co.fitstart.coach.handler.impl;

import org.springframework.stereotype.Component;

import co.fitstart.coach.handler.DistanceHandler;

/**
 * 
 * @author mohanlan
 *
 */
@Component("distanceHandler")
public class DistanceHandlerImpl implements DistanceHandler {
    
    private static final  double EARTH_RADIUS = 6378137;//赤道半径
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.mobile.handler.DistanceHandler#getDistance(double, double, double, double)
     */
    public int getDistance(double longitude, double latitude, double baseLongitude, double baseLatitude) {
        
        double radLatitude = rad(latitude);
        double radBaseLatitude = rad(baseLatitude);
        
        double latitudeBetween = radLatitude - radBaseLatitude;
        double longitudeBetween = rad(longitude) - rad(baseLongitude);
        
        double sin = 2 *Math.asin(Math.sqrt(Math.pow(Math.sin(latitudeBetween/2),2)+Math.cos(radLatitude)*Math.cos(radBaseLatitude)*Math.pow(Math.sin(longitudeBetween/2),2)));
        
        int distance = (int) (sin * EARTH_RADIUS);
        
       return distance;//单位米
    }
    
    /**
     * 
     * @param d
     * @return
     */
    private static double rad(double d){
        return d * Math.PI / 180.0;
    }
}
